Przykad 7.7. Realizacja algorytmu negmaks
public class NegMaxEvaluation implements IEvaluation {

   IGameState state;    /** Stan do modyfikowania podczas poszukiwa */
   int ply;             /** Gboko kursowania, czyli zasig poszukiwa */

   public NegMaxEvaluation (int ply) {
      this.ply = ply;
   }

   public IGameMove bestMove (IGameState s,
                              IPlayer player, IPlayer opponent) {
      this.state = s.copy();
      MoveEvaluation me = negmax(ply, player, opponent);
      return me.move;
   }

   public MoveEvaluation negmax (int ply, IPlayer player, IPlayer opponent) {

      // Jeli brak ruchw dozwolonych lub li, to zwr ocen stanu planszy
      Iterator<IGameMove> it = player.validMoves(state).iterator();
      if (ply == 0 || !it.hasNext()) {
         return new MoveEvaluation (player.eval(state));
      }

      // Prbuj poprawi ten ruch o dolnym ograniczeniu
      MoveEvaluation best = new MoveEvaluation (MoveEvaluation.minimum());

      // We ruchy danego gracza i wygeneruj wynikajce z nich plansze.
      // Wybierz maksimum z ujemnych ocen potomkw
      while (it.hasNext()) {
         IGameMove move = it.next();
         move.execute(state);

         // Oceniaj pozycj rekurencyjnie, jednolicie uywajc negmaks.
         // Traktuj ocen jako warto ujemn
         MoveEvaluation me = negmax (ply-1, opponent, player);
         move.undo(state);

         if (-me.score > best.score) {
            best = new MoveEvaluation (move, -me.score);
         }
      }
      return best;
   }
}
